﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelUserView :IView  {
	public Text enemysCount;
	public Text coinCount;
	public Text levelName;
	public int userhp=5;
	public List<GameObject> hpObj;
    public override void Init()
    {
        GameManager.Instance.onHpChange+=HpChange;
		GameManager.Instance.onCoinChange+=CoinChange;
		GameManager.Instance.onEnemyCountChange+=EnemyCountChange;
        LevelManager.Instance.changeLevel += LevelTitleUpdate;
		userhp=hpObj.Count;
		Show();
    }

    private void LevelTitleUpdate(LevelInfo obj)
    {
        levelName.text = obj.LevelTitle;
    }

    private void EnemyCountChange(int obj,int allCount)
    {
        enemysCount.text=obj.ToString()+"/"+allCount.ToString();
    }

    private void CoinChange(int obj)
    {
        coinCount.text=obj.ToString();
    }

    private void HpChange(int obj)  //5
    {
        userhp=Mathf.Max(obj,0);
		int count=hpObj.Count;
		for(int i= count-1; i>=0;i--){
            if (i < userhp)
            {
                hpObj[i].SetActive(true);
            }
            else
            {
                hpObj[i].SetActive(false);
            }
		}
       
    }
    private void OnDestroy() {
        GameManager.Instance.onHpChange-=HpChange;
		GameManager.Instance.onCoinChange-=CoinChange;
		GameManager.Instance.onEnemyCountChange-=EnemyCountChange;
        LevelManager.Instance.changeLevel -= LevelTitleUpdate;
    }
}
